package com.ruoyi.system.service.impl;

import com.ruoyi.common.enums.ViewRelationMetadataEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContainerAccount;
import com.ruoyi.system.domain.SecurityValveAccount;
import com.ruoyi.system.domain.ViewRelationMetadata;
import com.ruoyi.system.mapper.ContainerAccountMapper;
import com.ruoyi.system.service.ContainerAccountService;
import com.ruoyi.system.service.ViewRelationMetadataService;
import org.apache.commons.collections.MapUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 压力容器台账(ContainerAccount)表服务实现类
 *
 * @author makejava
 * @since 2022-12-15 10:14:01
 */
@Service("containerAccountService")
public class ContainerAccountServiceImpl extends ViewRelationMetadataAbstract<List<ContainerAccount>> implements ContainerAccountService {

    @Resource
    private ViewRelationMetadataService viewRelationMetadataService;
    @Resource
    private ContainerAccountMapper containerAccountDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ContainerAccount queryById(Integer id) {
        return this.containerAccountDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param containerAccount 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ContainerAccount> queryByPage(ContainerAccount containerAccount, PageRequest pageRequest) {
        long total = this.containerAccountDao.count(containerAccount);
        return new PageImpl<>(this.containerAccountDao.queryAllByLimit(containerAccount, pageRequest), pageRequest, total);
    }

    @Override
    public List<ContainerAccount> queryAll(ContainerAccount containerAccount) {
        List<ContainerAccount> containerAccounts = containerAccountDao.queryAll(containerAccount);
        return convert(containerAccounts);
    }

    /**
     * 新增数据
     *
     * @param containerAccount 实例对象
     * @return 实例对象
     */
    @Override
    public ContainerAccount insert(ContainerAccount containerAccount) {
        this.containerAccountDao.insert(containerAccount);
        return containerAccount;
    }

    /**
     * 修改数据
     *
     * @param containerAccount 实例对象
     * @return 实例对象
     */
    @Override
    public ContainerAccount update(ContainerAccount containerAccount) {
        this.containerAccountDao.update(containerAccount);
        return this.queryById(containerAccount.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.containerAccountDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<ContainerAccount> containerAccounts, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(containerAccounts) || containerAccounts.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        containerAccountDao.insertBatch(containerAccounts);
        return "成功";
    }

    @Override
    public List<ContainerAccount> convert(List<ContainerAccount> containerAccounts) {
        if (!CollectionUtils.isEmpty(containerAccounts)) {
            List<Integer> ids = containerAccounts.stream().map(ContainerAccount::getId).collect(Collectors.toList());
            Map<String, ViewRelationMetadata> relationMetadataMap = viewRelationMetadataService.queryMappingRelation(ViewRelationMetadataEnum.CONTAINER_ACCOUNT.name(), ids);
            if (MapUtils.isNotEmpty(relationMetadataMap)) {
                for (ContainerAccount values : containerAccounts) {
                    String key = ViewRelationMetadataEnum.CONTAINER_ACCOUNT.name() + values.getId().toString();
                    ViewRelationMetadata viewRelationMetadata = relationMetadataMap.get(key);
                    if (null != viewRelationMetadata) {
                        values.setMetadataFileName(viewRelationMetadata.getMetadataFileName());
                        values.setFileId(viewRelationMetadata.getFileId());
                        values.setFileName(viewRelationMetadata.getFileName());
                        values.setFileUrl(viewRelationMetadata.getFileUrl());
                    }
                }
            }
        }
        return containerAccounts;
    }
}
